package com.zsxb.crm.controller;

import com.zsxb.crm.annotation.Log;
import com.zsxb.crm.dto.DictDTO;
import com.zsxb.crm.dto.DictDetailDto;
import com.zsxb.crm.dto.DictDetailPageDto;
import com.zsxb.crm.dto.DictPageDTO;
import com.zsxb.crm.pojo.Dict;
import com.zsxb.crm.pojo.SysStatus;
import com.zsxb.crm.result.PageResult;
import com.zsxb.crm.result.Result;
import com.zsxb.crm.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/dict")
@Slf4j
public class DictController {
    @Autowired
    private DictService dictService;

    /**
     * 数据字典分页
     */
    @GetMapping("/type/list")
    public PageResult dictPageQuery(DictPageDTO dictPageDTO){
        log.info("查询字典条件：{}",dictPageDTO);
        return dictService.pageQuery(dictPageDTO);
    }

    /**
     * 字典类型新增
     */
    @PostMapping("type")
    @Log
    public Result<String> insert(@RequestBody DictDTO dictDTO){
        dictService.insert(dictDTO);
        return Result.success();
    }

    /**
     * 字典类型删除
     */
    @DeleteMapping("/type/{DictId}")
    @Log
    public Result<String> delete(@PathVariable String dictId){
        dictService.delete(dictId);
        return Result.success();
    }

    @DeleteMapping("/data/{dictCode}")
    @Log
    public Result<String> deleteDetail(@PathVariable String dictCode){
        dictService.deleteDetail(dictCode);
        return Result.success();
    }
    /**
     * 字典类型修改回显
     */
    @GetMapping("/type/{DictId}")
    public Result<Dict> queryByDictId(@PathVariable Long DictId){
        Dict dict = dictService.queryByDictId(DictId);
        return Result.success(dict);
    }

    /**
     * 字典类型修改回显
     */
    @PutMapping("/type")
    @Log
    public Result<String> update(@RequestBody Dict dict){
        log.info("修改的字典数据：{}",dict);
        dictService.update(dict);
        return Result.success();
    }

    /**
     * 根据字典类型查询字典数据
     * @param dict_type
     * @return
     */
    @GetMapping("/data/type/{dict_type}")
    public Result findByDictType(@PathVariable String dict_type){
        List<SysStatus> list = dictService.findByDictType(dict_type);
        return Result.success(list);
    }

    /**
     * 字典明细分页查询
     * @param dto
     * @return
     */
    @GetMapping("/data/list")
    public PageResult findDetailByDictType(DictDetailPageDto dto){
        PageResult list = dictService.findDetailByDictType(dto);
        return PageResult.success(list);
    }

    @PostMapping("/data")
    @Log
    public Result<String> addDictDetail(@RequestBody DictDetailDto dto){
        dictService.addDictDetail(dto);
        return Result.success();
    }
    @GetMapping("/data/{dictCode}")
    public Result<SysStatus> findByDictCode(@PathVariable Long dictCode){
        SysStatus sysStatus = dictService.findByDictCode(dictCode);
        return Result.success(sysStatus);
    }



    @PutMapping("/data")
    @Log
    public Result<String> updateDictDetail(@RequestBody SysStatus sysStatus){
        dictService.updateDictDetail(sysStatus);
        return Result.success();
    }


}
